package cn.geminis.warehouse.service.purchase.controller;

import cn.geminis.warehouse.service.purchase.entity.PurchaseCheckFailed;
import cn.geminis.warehouse.service.purchase.entity.filter.PurchaseDetailFilter;
import cn.geminis.warehouse.service.purchase.entity.repository.PurchaseDetailRepository;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

public class PurchaseCheckFailedController {

    private final PurchaseDetailRepository purchaseDetailRepository;

    @PostMapping
    public void save(@RequestBody PurchaseCheckFailed object) {
        var record = this.purchaseDetailRepository
                .findOne(
                        PurchaseDetailFilter.builder()
                                .purchaseId(object.getPurchase().getId())
                                .productBatchId(object.getProductBatch().getId())
                                .build()
                )
                .get();
        if (record.getAmountScan() - object.getAmount() < 0) {
            throw new RuntimeException("异常货物数量不足");
        } else {
            this.repository.save(object);
        }
    }

}
